<?php

/**
 * CustomerPanelController is the default controller for this application
 */
class CustomerPanelController extends BrowseControllerBase {
	/**
	 *
	 * UsersDataService Object
	 * @var UsersDataService Object
	 */
	private $usersDataService;
	
	/**
	 *
	 * CustomerOrderDataService Object
	 * @var CustomerOrderDataService Object
	 */
	private $customerOrderDataService;
	
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		$this->requireUserLogin ();
		
		//Check if User is logged in
		$currentUserId = $this->authentication->getCurrentUserId ();
		if ($currentUserId == null) {
			// Go on to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		//Check if Blogging Module is enabled
		if ($this->currentPackageOption) {
			$module = MODULE_ECOMMERCE;
			$isEnabled = $this->currentPackageOption->isEnabled ( $module );
			if ($isEnabled == false) {
				$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Index/errorAuthor';
				CommonUtils::redirect ( $url );
				return - 1;
			}
		}
		
		// Init all DataService
		$this->usersDataService = new UsersDataService ();
		
		$this->customerOrderDataService = new CustomerOrderDataService ();
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 *
	 */
	
	public function indexAction() {
		
		$arrElement = array ();
		$arrElement ['ownerId'] = $this->authentication->getCurrentUserId ();
		$arrElement ['userType'] = ROLE_MEMBER_USER;
		$itemList = $this->usersDataService->getItems ( $arrElement );
		
		$this->smarty->assign ( 'itemList', $itemList );
		$this->smarty->assign ( 'itemCount', count ( $itemList ) );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/CustomerPanel/list_customer.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function delAction() {
		// Get Id of page
		$id = $this->getQuery ( 'id', 0 );
		
		if (! empty ( $id ) && is_numeric ( $id )) {
			
			$id = intval ( $id );
			
			//Clean all other information
			$user_id = $id;
			$addressDataService = new AddressDataService ();
			$addressDataService->removeAddress ( $user_id );
			
			$userCustomerBasketDataService = new UserCustomerBasketDataService ();
			$userCustomerBasketDataService->removeCart ( $user_id, NULL );
			
			$customerOrderDataService = new CustomerOrderDataService ();
			$customerOrderDataService->removeOrder ( $user_id );
			
			// Delete page
			$res = $this->usersDataService->del ( $id );
		}
		
		// Redirect to edit page
		$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/CustomerPanel/index';
		CommonUtils::redirect ( $url );
		return;
	}
	
	/**
	 * View Orders Action Handling
	 */
	public function viewOrdersAction() {
		$id = $this->getQuery ( 'id', 0 );
		
		if (! empty ( $id ) && is_numeric ( $id )) {
			$arrElement = array ();
			$arrElement ['customers_id'] = $id;
			$arrElement [] = " orders_status > 0 ";
			
			//$numRecordPerPage = ITEM_PER_PAGE;
			$numRecordPerPage = 20;
			
			// Pager
			$start = $this->getQuery ( 'start', 0 );
			if ($start == '' || $start < 0) {
				$start = 0;
			}
			
			$totals = $this->customerOrderDataService->getTotals ( $arrElement );
			$itemList = $this->customerOrderDataService->getItems ( $arrElement, NULL, $start, $numRecordPerPage );
			
			if ($start > $totals) {
				$start = 0;
			}
			$link = Zend_Registry::get ( 'config' )->APPLICATION_URL . "/Browse/CustomerPanel/viewOrders/id/" . $id;
			$pageLink = '';
			if ($totals > $numRecordPerPage) {
				$pageUtils = new PagerUtils ( $start, $totals, $numRecordPerPage, 10, $link );
				$pageLink = $pageUtils->writePagesLinks ( 0, 'start' );
				$pageLink .= "<br><span class=\"paing_text_nolink\">" . $pageUtils->writePagesCounter () . "</span>";
			}
			
			$this->smarty->assign ( 'pageLink', $pageLink );
			$this->smarty->assign ( 'itemList', $itemList );
			$this->smarty->assign ( 'itemCount', count ( $itemList ) );
		}
		
		$currentUserId = $this->authentication->getCurrentUserId ();
		$currentUser = $this->usersDataService->getById ( $currentUserId );
		if ($currentUser) {
			$this->smarty->assign ( 'userHomePage', $currentUser->getHomeURL () );
		}
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/CustomerPanel/list_orders.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	/**
	 * Delete Order Action Handling
	 */
	public function delOrderAction() {
		// Get Id of page
		$id = $this->getQuery ( 'id', 0 );
		if (! empty ( $id ) && is_numeric ( $id )) {
			
			$id = intval ( $id );
			
			$customerOrderProductDataService = new CustomerOrderProductDataService ();
			$customerOrderProductDataService->removeOrder ( $id );
			
			//Del Order
			$res = $this->customerOrderDataService->del ( $id );
		}
		// Redirect to edit page
		$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/CustomerPanel/index';
		CommonUtils::redirect ( $url );
		return;
	}
	
	/**
	 * Update Status Action
	 */
	public function updateStatusAction() {
		$id = $this->getQuery ( 'id', 0 );
		if ($id == "") {
			$id = $this->getPost ( 'id', 0 );
		}
		if (! empty ( $id ) && is_numeric ( $id )) {
			
			$customerOrder = $this->customerOrderDataService->getById ( $id );
			//Get Current Status of Order
			$this->smarty->assign ( 'status', $customerOrder->orders_status );
			$this->smarty->assign ( 'id', $id );
			
			$actionForm = $this->getPost ( 'actionForm', 0 );
			$status = $this->getPost ( 'status', 0 );
			if (! empty ( $actionForm ) && $actionForm == 'submit' && $status != $customerOrder->orders_status && $this->isDemoMode == false) {
				
				$comment = $this->getPost ( 'comment', 0 );
				$customerOrder->orders_status = $status;
				$this->customerOrderDataService->update ( $customerOrder );
				
				//Add Product Order Status History
				$orderStatusHistoryDataService = new OrderStatusHistoryDataService ();
				$orderHistory = $orderStatusHistoryDataService->beforeCreate ();
				$orderHistory->orders_id = $customerOrder->id;
				$orderHistory->orders_status = $status;
				$orderHistory->date_added = date ( "Y-m-d H:i:s" );
				$orderHistory->comments = $comment;
				$orderStatusHistoryDataService->create ( $orderHistory );
				
				$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/CustomerPanel/index';
				CommonUtils::redirect ( $url );
				return;
			}
		} else {
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/CustomerPanel/index';
			CommonUtils::redirect ( $url );
			return;
		}
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/CustomerPanel/update_order_status.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
}
